import { h, render, VNode } from 'vue'
import MessageBox from './MessageBox.vue'

export type MessageBoxProps = {
    message: string,
    delay: number
}

const renderBox: () => VNode = () => {
    const props: MessageBoxProps = {
        message: '',
        delay: 1000
    } as any
    const container = document.createElement('div')
    const boxVNode = h(MessageBox, props)
    render(boxVNode, container)
    document.body.appendChild(container)
    return boxVNode
}

let boxInstance: any
const MessageBoxFun = (props: MessageBoxProps): void => {
    if (boxInstance) {
        const boxVue = boxInstance.component
        boxVue.proxy.open(props.message, props.delay)
    } else {
        boxInstance = renderBox()
        MessageBoxFun(props)
    }
}

export default MessageBoxFun